﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotNetAgent.Core;
using System.ComponentModel;

namespace DotNetAgent
{
	class PingAgent : Agent
	{
		int Pings = 0;
		int MaxPings = 5;
		String TargetAgent = null;

		public PingAgent(Agent p_Owner, Dictionary<String, Object> p_Args)
			: base(p_Owner, p_Args)
		{
			if (!p_Args.ContainsKey("TargetAgent"))
			{
				throw new AgentException("Activation", "TargetAgent not specified");
			}
			TargetAgent = p_Args["TargetAgent"] as String;
			AddBehaviour(Ping);
			AddBehaviour(SayHi);
		}
		protected void Ping(Dictionary<String, Object> p_Args)
		{
			try
			{
				if (Pings < MaxPings)
				{
					Send(TargetAgent,"Ping",null);
				}
				Sleep(new Random(DateTime.Now.Millisecond + i_Name.GetHashCode()).Next(5000));
				Pings++;

			}
			catch (Exception ex)
			{
				LogError("Ping", "Error While Pinging", ex);
				Stop(false, false);
			}
		}
		protected void SayHi(Dictionary<String, Object> p_Args)
		{
			try
			{
				if (Pings < MaxPings)
				{
					Send(TargetAgent, "Hi!", null);
				}
				Sleep(new Random(DateTime.Now.Millisecond + i_Name.GetHashCode()).Next(5000));
				Pings++;

			}
			catch (Exception ex)
			{
				LogError("SayHi", "Error While Saying Hi", ex);
				Stop(false, false);
			}
		}
	}
}
